package com.example.administrator.myapplication.activity.palyActivity;

import android.databinding.DataBindingUtil;
import android.media.MediaPlayer;
import android.support.v4.util.Pair;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.example.administrator.myapplication.BR;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.activity.palyActivity.bean.PlayEntity;
import com.example.administrator.myapplication.callback.XmlyService;
import com.example.administrator.myapplication.databinding.ActivityPlayBinding;
import com.example.administrator.myapplication.until.BaseApplication;
import com.example.administrator.myapplication.until.CommentRecyclerAdapter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PlayActivity extends AppCompatActivity implements Callback<PlayEntity> {
   private ActivityPlayBinding binding;
    private MediaPlayer mediaPlayer;
    private String url;
    private String trackId;
    private String uid;
    private String title;
    private CommentRecyclerAdapter<Object> adapter;
    private XmlyService service;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_play);
        aboutIntentExtra();
        aboutRecycler();
    }
    private void aboutRecycler() {
//     HashMap<Class<?>,Pair<Integer,Integer>> map = new HashMap<>();
//        map.put(null,Pair.create(R.layout.play_item_paly, BR.playEntity));
//     adapter = new CommentRecyclerAdapter<>(this,new ArrayList<>(),map);
//       binding.recycler.setAdapter(adapter);
        service = ((BaseApplication) getApplicationContext()).getService();
        service.getPlayEntity(trackId,uid).enqueue(this);
    }

    private void aboutIntentExtra() {
        url = getIntent().getStringExtra("playUrl64");
        trackId = getIntent().getStringExtra("trackId");
        uid = getIntent().getStringExtra("uid");
        title = getIntent().getStringExtra("title");
        binding.title.setText(title);

    }
    private void aboutMediaPlyer() {
        mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(url);
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onResponse(Call<PlayEntity> call, Response<PlayEntity> response) {
        //  adapter.add(response.body());
        binding.setPlayEntity1(response.body());
          aboutMediaPlyer();
    }

    @Override
    public void onFailure(Call<PlayEntity> call, Throwable t) {

    }
}
